#!/bin/bash

# 检查参数
if [ "$#" -ne 1 ]; then
    echo "Usage: $0 <backup_file>"
    echo "Example: $0 /backup/backup_20230101_120000.tar.gz"
    exit 1
fi

BACKUP_FILE=$1
TEMP_DIR="/tmp/restore_$(date +%s)"

# 检查备份文件是否存在
if [ ! -f "$BACKUP_FILE" ]; then
    echo "Backup file not found: $BACKUP_FILE"
    exit 1
fi

# 解压备份文件
echo "Extracting backup file..."
mkdir -p "${TEMP_DIR}"
tar xzf "${BACKUP_FILE}" -C "${TEMP_DIR}"

# 恢复MySQL数据
echo "Restoring MySQL database..."
gunzip < "${TEMP_DIR}"/*/mysql_backup.sql.gz | docker exec -i worktool-mysql mysql -uroot -pyour_password worktool

# 恢复Redis数据
echo "Restoring Redis data..."
docker cp "${TEMP_DIR}"/*/redis_backup.rdb worktool-redis:/data/dump.rdb
docker restart worktool-redis

# 恢复配置文件
echo "Restoring configuration files..."
cp -r "${TEMP_DIR}"/*/config/* config/

# 清理临时文件
rm -rf "${TEMP_DIR}"

echo "Restore completed" 